<?php

namespace app\admin\controller\order;

use app\api\controller\Distance;
use app\common\controller\Api;
use app\common\controller\Backend;
use app\common\library\Wxmini;
use app\common\model\Goods;
use think\Controller;
use think\Db;
use think\exception\DbException;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 订单管理
 *
 * @icon fa fa-circle-o
 */
class Order extends Backend
{

    /**
     * Order模型对象
     * @var \app\common\model\order\Order
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\order\Order;
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("payStatusList", $this->model->getPayStatusList());
        $this->view->assign("orderStatusList", $this->model->getOrderStatusList());
        $this->view->assign("refundStatusList", $this->model->getRefundStatusList());
        $this->view->assign("isAppointList", $this->model->getIsAppointList());
        $this->view->assign("payTypeList", $this->model->getPayTypeList());
        $this->view->assign("platformList", $this->model->getPlatformList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['user','orderitem'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                
                $row->getRelation('user')->visible(['nickname','mobile','avatar']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }
    public function detail()
    {
        if ($this->request->isPost()) {
            $param = input('');
            Db::startTrans();

            $id = input('post.ids');
            $row = $this->model->get($id);
            $before_checkinfo = input('before_checkinfo');
            $checkinfo = input('checkinfo');
            $is_pay=false;
            $org = [];
            $checkinfoList = $this->model->checkinfoList;
            $is_cancel=false;
            $params = input("row/a");

            if($before_checkinfo==-2&&$checkinfo!=$before_checkinfo){
                $this->error('已退款订单不可以操作');
            }
         //   ['待付款','待预约','已完成','全部退款','已取消'];



            if(isset($checkinfo)){
                if ($before_checkinfo != $checkinfo) {

                    $org = [
                        '订单状态由' . $checkinfoList[$before_checkinfo] . '改成' . $checkinfoList[$checkinfo],
                    ];

                }
            }else{
                $checkinfo=$before_checkinfo;
            }



            if($checkinfo==1){
                $order_status=$this->model->where('id',$id)->find();
                if(!$order_status){
                    $this->error('订单不存在');
                }
                if($order_status['status']==-1){
                    $this->error('订单已取消');
                }
                if($order_status['status']>0){
                    $this->error('订单已支付');
                }
            }
            if($checkinfo==3&&$before_checkinfo!=$checkinfo){
                $this->error('退款去退款订单中处理');
            }
            if($before_checkinfo==0&&$checkinfo!=$before_checkinfo){
                if($checkinfo!=4){
                    $this->error('不可操作');
                }
            }


            //  ['待付款','待预约','已完成','全部退款','已取消'];
            switch ($checkinfo) {

                case 1:
                    //待预约
                    if($before_checkinfo!=$before_checkinfo){
                       // $row['status']=$row[''];
                    }

                    break;
                case 2:
                    //已完成
                    if($row['app'])
                    $row['type'] = 1;

                    break;
                //已取消
                case 4:
                    $row['order_status'] =-1;
                    $row['status']=-1;
                    $row['cancel_time'] = time();

                    break;

            }








            if (!empty($org)) {
                $this->modify_log($org, $row->id);
            }



            $res=$row->save();

            if($res){

                Db::commit();
                $this->success('');
            }else{
                Db::rollback();
            }






            $this->success();
        }

        $param = $this->request->param();

        $row = $this->model->with(['orderitem'])->where('id', $param['ids'])->find();
        $row['log']=\db('order_log')->where(['order_id'=>$param['ids']])->select();
        $row->checkinfo =$this->getCheckinfo($row);


        $this->assign('checkinfoList', $this->model->checkinfoList);
        $this->view->assign('vo', $row);

        return $this->view->fetch();
    }
    public function getCheckinfo($row)
    {
       //     public $checkinfoList = ['待付款','待预约','已完成','全部退款','已取消'];
        $checkinfo=0;
        if($row['order_status']==-1){
            $checkinfo=4;
        }else if($row['order_status']==0){
            $checkinfo=0;
        }else if($row['order_status']==1){
            if($row['appoints']==$row['used_appoints']){
                $checkinfo=2;
            }else{
                $checkinfo=1;
            }
        }else if($row['order_status']==-2){
            $checkinfo=3;
        }
        return $checkinfo;
    }
    //修改日志记录
    public function modify_log($row,$order_id,$type='order'){

        $data = [
            'order_id'      =>  $order_id,
            'mod_content'   =>  implode('、',$row),
            'admin_id'      =>  session('admin.id'),
            'type'          =>  $type,
            'createtime'    =>  time(),
            'updatetime'    =>  time(),
        ];
        db('order_log')->insert($data);

    }
}
